package test;

// 已知一个数组int oldArray[]={7,8,0,4,3,0,9,1,0,6,2,5},将该数组中值为0的数组元素去除后形成一个新数组int newArray[]，
// 计算新数组中数组元素的平均值，最后输出原数组内容、新数组内容及新数组元素的平均值
// 使用打擂台的方式求出上面数组的最大最小值

public class exam01 {
    public static void main(String[] args) {
        int[] oldArray = new int[]{7, 8, 0, 4, 3, 0, 9, 1, 0, 6, 2, 5};
        int sum = 0;
        int avgOld = 0;
        int avgNew = 0;
        int maxOld = 0;
        int maxNew = 0;

        int len = 0;

        maxOld = oldArray[0];
        for (int i = 0; i < oldArray.length; i++) {
            if (oldArray[i] != 0) {
                len++;
            }
            sum += oldArray[i];
            if (maxOld < oldArray[i]) {
                maxOld = oldArray[i];
            }
        }
        avgOld = sum / oldArray.length;

        int[] newArray = new int[len];
        int t = 0;
        for (int i = 0; i < oldArray.length; i++) {
            if (oldArray[i] != 0) {
                newArray[t] = oldArray[i];
                sum += newArray[t];
                if (maxNew < newArray[t]) {
                    maxNew = newArray[t];
                }
                t++;
            }
        }
        avgNew = sum / newArray.length;


        for (int i = 0; i < newArray.length; i++) {
            System.out.print(newArray[i] + " ");
        }
        System.out.println("\n" + "老数组平均值：" + avgOld);
        System.out.println("新数组平均值：" + avgNew);
        System.out.println("老数组Max " + maxOld);
        System.out.println("新数组Max " + maxNew);

    }
}
